package com.shqcjd.sinopecdemomobile.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shqcjd.sinopecdemomobile.R;
import com.shqcjd.sinopecdemomobile.api.ApiService;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxObserver;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.sinopecdemomobile.model.ResponseData;
import com.shqcjd.sinopecdemomobile.model.pojo.MoreImgListBean;
import com.shqcjd.sinopecdemomobile.ui.activity.preview.PreImageActivity;
import com.shqcjd.sinopecdemomobile.ui.activity.preview.VideosActivity;
import com.shqcjd.sinopecdemomobile.ui.adapter.MoreContentAdapter;
import com.shqcjd.sinopecdemomobile.ui.base.BaseActivity;
import com.shqcjd.sinopecdemomobile.ui.base.BasePresenter;
import com.shqcjd.sinopecdemomobile.util.UIUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

public class MoreContentActivity extends BaseActivity {
    @BindView(R.id.tv_include_title)
    TextView tvIncludeTitle;
    @BindView(R.id.tv_include_finish)
    Button tvIncludeFinish;
    @BindView(R.id.rv_more_content)
    RecyclerView rvMoreContent;
    @BindView(R.id.sr_more_content)
    SwipeRefreshLayout srMoreContent;
    List<String> mList = new ArrayList<>();

    private Context context;
    private int tId, eId;
    private int page = 1;
    private MoreContentAdapter moreContentAdapter;
    
    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_more_content;
    }

    @Override
    public void init() {
        super.init();
        tId = getIntent().getIntExtra("tId", 0);
        eId = getIntent().getIntExtra("eId", 0);
    }

    @Override
    public void initView() {
        super.initView();
        context = this;
        tvIncludeTitle.setText("更多内容");

        tvIncludeFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        srMoreContent.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshData();
            }
        });

        refreshData();
    }

    private void refreshData() {
        srMoreContent.setRefreshing(true);
        page = 1;
        mList.clear();
        getDataList(page);
    }

//    private void loadMoreData() {
//        isRefreshOrLoadMore = 1;
//        getDataList(page + 1);
//    }

//    private void refreshOrLoadMore(){
//        if(moreContentAdapter!=null){
//            moreContentAdapter.notifyLoadMoreToLoading();
//        }
//    }

    private void getDataList(int p) {
        Map<String, String> map = new HashMap<>();
        map.put("pageIndex",p+"");
        map.put("pageSize", 99 +"");
        map.put("task_id", tId+"");
        map.put("equipment_id",eId+"");
        ApiService.playBackMoreImgHttp(new JSONObject(map))
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<MoreImgListBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                    }

                    @Override
                    public void _onNext(ResponseData<MoreImgListBean> responseData) {
                        if (responseData.retCode != 10000) {
                            return;
                        }
                        mList.addAll(responseData.data.items);
                        if (moreContentAdapter == null) {
                            rvMoreContent.setLayoutManager(new GridLayoutManager(context, 3));
                            moreContentAdapter = new MoreContentAdapter(mList, context);
                            rvMoreContent.setAdapter(moreContentAdapter);
                            moreContentAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                                    if(UIUtils.getFormatName((String)adapter.getData().get(position)).equals(".mp4")){
                                        Intent it = new Intent(context, VideosActivity.class);
                                        it.putExtra("url", (String)adapter.getData().get(position));
                                        it.putExtra("name", "视频详情");
                                        context.startActivity(it);
                                    }else{
                                        Intent it = new Intent(context, PreImageActivity.class);
                                        it.putExtra("url", (String)adapter.getData().get(position));
                                        it.putExtra("name", "图片详情");
                                        context.startActivity(it);
                                    }
                                }
                            });
//                            moreContentAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
//                                @Override
//                                public void onLoadMoreRequested() {
//                                    rvMoreContent.post(new Runnable() {
//                                        @Override
//                                        public void run() {
//                                            int pageSize = responseData.data.total%15==0?responseData.data.total/15:responseData.data.total/15+1;
//                                            if (page >= pageSize) {
//                                                moreContentAdapter.loadMoreEnd();
//                                            } else {
//                                                loadMoreData();
//                                            }
//                                        }
//                                    });
//                                }
//                            });
                        }
                        moreContentAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        srMoreContent.setRefreshing(false);
                    }
                });
    }
}
